package com.base.caselib;

import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Layout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.caselib.adapter.ChooseRoadShowAdapter;
import com.base.caselib.bean.PaintEvent;
import com.base.caselib.databinding.ActivityPaintBinding;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.config.MvpConstants;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ScreenUtil;
import com.bumptech.glide.Glide;
import com.xiaopo.flying.sticker.Sticker;
import com.xiaopo.flying.sticker.StickerView;
import com.xiaopo.flying.sticker.TextSticker;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by arvin on 2018/11/4.
 */
@Route(path = "/paint/paint")
public class PaintActivity extends CBaseBarMvpActivity implements View.OnClickListener {

    private ActivityPaintBinding mBinding;
    private String mRoadName1;
    private String mRoadName2;
    private String mReference1;
    private String mReference2;
    private SimpleDateFormat mDateFormat;


    @Override
    public void setOnOrientation() {

    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityPaintBinding) mRootBinding;
        mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        setBackNavigation();
        setBarTitle("绘制示意图");
        mBinding.imgRight.setImageResource(R.drawable.ic_aye_w);
        mBinding.imgRight.setOnClickListener(this);
        mBinding.a.setOnClickListener(this);
        mBinding.b.setOnClickListener(this);
        mBinding.c.setOnClickListener(this);
        mBinding.road.setOnClickListener(this);
    }

    @Override
    protected void start() {
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        ChooseRoadShowAdapter adapter = new ChooseRoadShowAdapter(mContext);
        adapter.setClickListener(new ChooseRoadShowAdapter.ClickListener() {
            @Override
            public void onClickListener(int position) {
                initSticker(position);
            }
        });
        mBinding.rv.setAdapter(adapter);
        adapter.setData();

    }

    private  void initSticker(int position)
    {
        mBinding.rv.setVisibility(View.GONE);
        mBinding.imgRight.setVisibility(View.VISIBLE);
        mBinding.llSticker.setVisibility(View.VISIBLE);
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            PaintActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        mBinding.sticker.setBackgroundResource(position);
        mBinding.sticker.setLocked(false);
        mBinding.sticker.setConstrained(true);
        mBinding.sticker.configDefaultIcons();

        mBinding.sticker.setOnStickerOperationListener(new StickerView.OnStickerOperationListener() {
            @Override
            public void onStickerAdded(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerClicked(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerDeleted(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerDragFinished(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerTouchedDown(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerZoomFinished(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerFlipped(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerDoubleTapped(@NonNull Sticker sticker) {

            }
        });
    }


    private void addTextStiker() {
        final TextSticker sticker = new TextSticker(this);
        View view = LayoutInflater.from(this).inflate(R.layout.view_edit, null);
        final EditText editText = (EditText) view.findViewById(R.id.edt_idea);
        new AlertDialog.Builder(this).setTitle("请输入路名").setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (TextUtils.isEmpty(editText.getText())) {
                            showMes("必须输入路名！");
                            return;
                        }


                        if(TextUtils.isEmpty(mRoadName1))
                        {
                            mRoadName1 =editText.getText().toString();
                        }else
                        {
                            mRoadName2 =editText.getText().toString();
                        }

                        sticker.setDrawable(ContextCompat.getDrawable(getApplicationContext(),
                                R.drawable.sticker_transparent_background));
                        sticker.setText(editText.getText().toString());
                        sticker.setTextColor(Color.BLACK);
                        sticker.setTextAlign(Layout.Alignment.ALIGN_CENTER);
                        sticker.resizeText();
                        mBinding.sticker.addSticker(sticker);
                    }
                }).create().show();

    }


    private void addFlagBuilding(final String s) {
        if ("A".equals(s)) {
            setFlagBuilding(s, null);
            return;
        }
        View view = LayoutInflater.from(this).inflate(R.layout.view_edit, null);
        final EditText editText = (EditText) view.findViewById(R.id.edt_idea);
        new AlertDialog.Builder(this).setTitle("请输入参照物描述").setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        setFlagBuilding(s, editText);
                    }
                }).create().show();


    }


    private void setFlagBuilding(String s, EditText editText) {
        final TextSticker sticker = new TextSticker(this);
        sticker.setDrawable(ContextCompat.getDrawable(getApplicationContext(),
                R.drawable.sticker_stroke_bg));
        sticker.setText(s);
        sticker.setTextColor(Color.BLACK);
        sticker.setTextAlign(Layout.Alignment.ALIGN_CENTER);
        sticker.resizeText();
        mBinding.sticker.addSticker(sticker);
        if ("B".equals(s)) {
            mReference1 = editText.getText().toString();
        } else if ("C".equals(s)) {
            mReference2 = editText.getText().toString();
        }
    }



    @Override
    public void onClick(View v) {
        int i = v.getId();
       if (i == R.id.img_right) {
            save();

        } else if (i == R.id.a) {
            addFlagBuilding("A");

        } else if (i == R.id.b) {
            addFlagBuilding("B");

        } else if (i == R.id.c) {
            addFlagBuilding("C");

        } else if (i == R.id.road) {
            addTextStiker();

        }
    }



    private void save() {
        File rootFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ MvpConstants.TEMPPATH);
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }
        File file = new File(rootFile, mDateFormat.format(new Date()) + ".jpg");
        mBinding.sticker.save(file);
        if (file.exists()) {
            PaintEvent paintEvent = new PaintEvent();
            paintEvent.setPath(file.getAbsolutePath());
            paintEvent.setRoad1(mRoadName1);
            paintEvent.setRoad1(mRoadName2);
            paintEvent.setRefrence1(mReference1);
            paintEvent.setRefrence2(mReference2);
            RxBus2.getInstance().post(paintEvent);
            finish();
        } else {
            showError();
        }

    }


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_paint;
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO: 2018/12/26重新测量尺寸相关参数。
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
        {
            ScreenUtil.adaptScreen4VerticalSlide(this,360);
        }
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            ScreenUtil.adaptScreen(this,360,false);
        }

    }

    @Override
    public void onBackPressedSupport() {
        if (mBinding.rv.getVisibility() == View.GONE) {
            // TODO: 2018/3/20
            if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                PaintActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            mBinding.rv.setVisibility(View.VISIBLE);
            mBinding.imgRight.setVisibility(View.INVISIBLE);
            mBinding.llSticker.setVisibility(View.GONE);


        } else {
            super.onBackPressedSupport();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
    }


}
